package flc.ast.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.MyCalendarBean;
import flc.ast.bean.WorkTimeBean;
import flc.ast.utils.MyDatabase;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class CalendarAdapter extends StkProviderMultiAdapter<MyCalendarBean> {

    public CalendarAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(60));
        addItemProvider(new CalendarProvider());
    }

    private class CalendarProvider extends BaseItemProvider<MyCalendarBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_calendar;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyCalendarBean bean) {
            String format = "%.1f";
            baseViewHolder.setText(R.id.tvCalendarItemYangLi, bean.getDate1());
            baseViewHolder.setText(R.id.tvCalendarItemNongLi, bean.getDate2());
            long date = TimeUtils.string2Millis(bean.getDate(), new SimpleDateFormat("yyyy年MM月dd日"));
            long now = TimeUtils.getNowMills();
            List<WorkTimeBean> list = MyDatabase.getInstance(getContext()).getWorkTimeDao().getTodayWorkTime(date);
            if (list.size() > 0) {
                baseViewHolder.setBackgroundColor(R.id.llCalendarItemDate, 0);
                baseViewHolder.getView(R.id.tvCalendarItemDuration).setVisibility(View.VISIBLE);
                baseViewHolder.getView(R.id.tvCalendarItemIncome).setVisibility(View.VISIBLE);
                double duration = 0, income = 0;
                if (list.get(0).isLeave()) {
                    for (WorkTimeBean workTimeBean : list) {
                        if (workTimeBean.isLeave()) {
                            duration += workTimeBean.getDuration();
                        }
                        baseViewHolder.setBackgroundColor(R.id.llCalendarItem, Color.parseColor("#C3F0CA"));
                        baseViewHolder.setText(R.id.tvCalendarItemDuration, getContext().getString(R.string.leave_text));
                        baseViewHolder.setText(R.id.tvCalendarItemIncome, getContext().getString(R.string.hour_num, String.format(format, duration)));
                        baseViewHolder.setTextColor(R.id.tvCalendarItemDuration, Color.parseColor("#41B951"));
                        baseViewHolder.setTextColor(R.id.tvCalendarItemIncome, Color.parseColor("#41B951"));
                    }
                } else {
                    for (WorkTimeBean workTimeBean : list) {
                        if (!workTimeBean.isLeave()) {
                            duration += workTimeBean.getDuration();
                            income += workTimeBean.getIncome();
                        }
                    }
                    baseViewHolder.setBackgroundColor(R.id.llCalendarItem, Color.parseColor("#D8E0FF"));
                    baseViewHolder.setText(R.id.tvCalendarItemDuration, getContext().getString(R.string.hour_num, String.format(format, duration)));
                    baseViewHolder.setText(R.id.tvCalendarItemIncome, getContext().getString(R.string.unit_rmb) + String.format(format, income));
                    baseViewHolder.setTextColor(R.id.tvCalendarItemDuration, Color.parseColor("#5476FC"));
                    baseViewHolder.setTextColor(R.id.tvCalendarItemIncome, Color.parseColor("#C57557"));
                }
            } else {
                baseViewHolder.getView(R.id.tvCalendarItemDuration).setVisibility(View.GONE);
                baseViewHolder.getView(R.id.tvCalendarItemIncome).setVisibility(View.GONE);
                if (date <= now) {
                    baseViewHolder.setBackgroundColor(R.id.llCalendarItem, 0);
                    baseViewHolder.setBackgroundColor(R.id.llCalendarItemDate, Color.parseColor("#F0F4FF"));
                    baseViewHolder.setTextColor(R.id.tvCalendarItemYangLi, Color.parseColor("#000000"));
                    baseViewHolder.setTextColor(R.id.tvCalendarItemNongLi, Color.parseColor("#A0A0A0"));
                } else {
                    baseViewHolder.setBackgroundColor(R.id.llCalendarItem, 0);
                    baseViewHolder.setBackgroundColor(R.id.llCalendarItemDate, 0);
                    baseViewHolder.setTextColor(R.id.tvCalendarItemYangLi, Color.parseColor("#66000000"));
                    baseViewHolder.setTextColor(R.id.tvCalendarItemNongLi, Color.parseColor("#66A0A0A0"));
                }
            }

            if (bean.isSelect()) {
                if (list.size() > 0) {
                    baseViewHolder.getView(R.id.ivCalendarItemSel).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.ivCalendarItemDateSel).setVisibility(View.GONE);
                } else {
                    baseViewHolder.getView(R.id.ivCalendarItemSel).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.ivCalendarItemDateSel).setVisibility(View.VISIBLE);
                }
            } else {
                baseViewHolder.getView(R.id.ivCalendarItemSel).setVisibility(View.GONE);
                baseViewHolder.getView(R.id.ivCalendarItemDateSel).setVisibility(View.GONE);
            }

            if (TextUtils.isEmpty(bean.getDate())) {
                baseViewHolder.getView(R.id.llCalendarItem).setVisibility(View.GONE);
            }
        }
    }
}
